/* eslint-disable @typescript-eslint/consistent-type-assertions */
/* eslint-disable import/no-anonymous-default-export */

import {
    cMap, CView, CFill,
    CStyle, CStroke, Cget,
    CVectorLayer, CVector,
    CSelect, Cclick, CDraw,
    createBox, DoubleClickZoom
} from "../ol";
import { mouseLngLat } from "../ol/tools";
import Layer from "../layers";

let documents: any = document;
/**
 * @params {
    * id( 地图渲染DOM节点id ),
    * layers( 初始化地图图层数据 ),
    * coordinates( 初始化坐标 ),
    * projection( 初始化坐标系 ),
    * zoom( 初始化地图级别 )
* }
 */

let maps: any;

export default class CMap {
    constructor({ id, layers, coordinates, projection, zoom }: any) {
        if (!id) return;

        let layer = new Layer();

        maps = new cMap({
            view: new CView({
                center: coordinates || [119.34095450080773, 28.207580551663552],
                projection: projection || Cget("EPSG:4326"),  //指定投影采用4326坐标系 119.91028, 28.40652
                zoom: zoom || 9.8
            }),
            layers: layers ? layers : [layer.init()],
            target: id,

        });

        // Initialize add scroll event
        maps.MouseScroll = (enents: any) => {

            // The original event parameter increases latitude and longitude
            const enent = (e: any) => {

                let lnglat = mouseLngLat([e.layerX, e.layerY], maps);

                e.longitude = lnglat[0];
                e.latitude = lnglat[1];

                enents(e);
            }

            if (documents.addEventListener) {
                documents.addEventListener('DOMMouseScroll', enent, false);
            };
            documents.getElementById(id).onmousewheel = enent;
        }
        // Initialize add scroll event

        maps.addInteractions = (type: string) => {
            let select;
            switch (type) {
                case 'select':
                    select = selectPoint();
                    maps.addInteraction(select)
                    break;
                case 'draw':
                    select = drawPoint();
                    maps.addInteraction(select.draw)
                    break;

                default:
                    break;
            }

            return select;
        }
        maps.removeInteractions = (type: string, select: any) => {
            switch (type) {
                case 'select':
                    maps.removeInteraction(select)
                    break;
                case 'draw':
                    maps.removeInteraction(select)
                    break;

                default:
                    break;
            }

        }
        /** 
         * //事件类型
            let type = {
                click:'click',//单击
                dblclick:'dblclick',//双击，双击会触发click
                singleclick:'singleclick',//单击，延迟250毫秒，就算双击不会触发
                moveend:'moveend',//鼠标滚动事件
                pointermove:'pointermove',//鼠标移动事件
                pointerdrag:'pointerdrag',//鼠标拖动事件
                precompose:'precompose',//地图准备渲染，为渲染
                postcompose:'postcompose',//地图渲染中
                postrender:'postrender',//地图渲染全部结束
                changeLayerGroup:'change:layerGroup',//地图图层增删时触发
                changeSize:'change:size',//地图窗口发生变化就会触发
                changeTarget:'change:target',//地图绑定的div发生更改时触发
                changeView:'change:view',//地图view对象发生变化触发
                propertychange:'propertychange',//Map对象中任意的property值改变时触发
            }
        */
        maps.MapOn = (type: string, enents: any) => {
            // The original event parameter increases latitude and longitude
            (<any>window)['enent'] = (e: any) => {
                e.longitude = e.coordinate[0];
                e.latitude = e.coordinate[1];
                enents(e);
            }
            maps.on(type, (<any>window)['enent']);
        }

        const dblClickInteraction = maps
            .getInteractions()
            .getArray()
            .find((interaction: any) => {
                return interaction instanceof DoubleClickZoom;
            });
        maps.removeInteraction(dblClickInteraction);

        // Return map object
        return maps;
    }
    TDT = () => {

    }

}

// 点击获取图层添加信息

const selectPoint = () => {
    return new CSelect({
        condition: Cclick,
    });
}
// 点击获取图层添加信息

const drawPoint = () => {

    let source = new CVector();
    let draw = new CDraw({
        source: source,
        type: 'Circle',
        geometryFunction: createBox()  // 绘制矩形
    });
    let Layer = new CVectorLayer({					//定义vector图层，将绘制的图形加入到vector图层，叠加在地图上展示
        source: source,
        style: new CStyle({
            //线条样式
            stroke: new CStroke({
                color: '#2649f6',
                width: 2
            }),
            //填充
            fill: new CFill({
                color: 'rgba(37,241,239,0.2)'
            })
        }),
    });
    return { draw, Layer }

}

